function loadShaders() {
	var elements = document.getElementsByTagName("script");
	for (var i=0;i< elements.length;i++) {
		if (elements[i].type.slice(0,8) == "x-shader") {
			if (window.XMLHttpRequest) {
				var xmlhttp = new XMLHttpRequest();
				var element = elements[i];
				
				//xmlhttp.onreadystatechanged = function() xmlhttpChange(xmlhttp, element);
				xmlhttp.open("GET", "Shaders/" + element.id + ".glsl", false);
				xmlhttp.send();
				element.innerHTML = xmlhttp.responseText;
			}
		} 
	}
}


function handleLoadedobject(objectData, index, type) {
				
		var object; 
		
		if (type == "fur") 
			object = furObj[index];
		else
			object = planeObj;

						
		object.normalBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, object.normalBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(objectData.normals), gl.STATIC_DRAW);
		object.normalBuffer.itemSize = 3;
		object.normalBuffer.numItems = objectData.normals.length / 3;
		object.texBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, object.texBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(objectData.uvs), gl.STATIC_DRAW);
		object.texBuffer.itemSize = 2;
		object.texBuffer.numItems = objectData.uvs.length / 2;
		object.positionBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, object.positionBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(objectData.positions), gl.STATIC_DRAW);
		object.positionBuffer.itemSize = 3;
		object.positionBuffer.numItems = objectData.positions.length / 3;
		object.indexBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.indexBuffer);
		gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(objectData.indices), gl.STATIC_DRAW);
		object.indexBuffer.itemSize = 1;
		object.indexBuffer.numItems = objectData.indices.length;
				
		object.loaded = true;
				
		document.getElementById("loadingtext").textContent = "";
}

function loadObjects() {
	
		//load plane
		var requestPlane = new XMLHttpRequest();
		requestPlane.open("GET", "TesselatedPlane.json");
		requestPlane.onreadystatechange = function() {	
	
		if (requestPlane.readyState == 4){
			handleLoadedobject(JSON.parse(requestPlane.responseText), 0, "plane");
		}
		};
		requestPlane.send();
	
		for (var i=0;i<furObj.length;i++) {
				var request = new XMLHttpRequest();
				request.i = i;
				request.open("GET", furObj[i].filename);
				request.onreadystatechange = function() {
				if(request.readyState == 4) {
					handleLoadedobject(JSON.parse(request.responseText), request.i, "fur");
				}
				};
				request.send();
					
				/*load textures for model*/
				var ti = furObj[i].texture_files;
				for (var j=0;j<ti.length;j++) {
					var name = ti[j];
					if (all_textures[name] != null) {
						furObj[i].textures[furObj[i].textures.length] = all_textures[name];
						continue;
					}
						
					var texture;
					texture = gl.createTexture();
					texture.image = new Image();
					texture.image.parent = texture;
					texture.image.onload = function (event) {
						handleLoadedTexture(event.currentTarget.parent);
					}
					texture.image.src = name;
						
					furObj[i].textures[furObj[i].textures.length] = texture;
					all_textures[name] = texture;
				}
					
		}
}

var cubemap_texture = function() {var object;object.loaded=0; return object};
function loadCubeTextures() {
	cubemap_texture = gl.createTexture();
	for (var i=0;i<6;i++)
	{
		var texture = new Object();
		texture.image = new Image();
		texture.image.i = i;
		texture.image.onload = function (event) {
			handleLoadedCubeTexture(event.currentTarget);
		}
		texture.image.src = "Images/g" + i + ".png";
	}
}
			
function handleLoadedCubeTexture(texture) {
	cubemap_texture.loaded++;
	gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
	gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubemap_texture);

	gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + texture.i ,0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture);
	//gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + texture.i, 0, gl.RGBA, 1024, 1024, 0, gl.RGBA, gl.UNSIGNED_BYTE, texture);
	gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR_MIPMAP_LINEAR);
	gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
	gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.MIRRORED_REPEAT);
	gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT);
	gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
	
}

var noise_texture;
function loadOtherTextures() {
	noise_texture = gl.createTexture();
	noise_texture.image = new Image();
	noise_texture.image.parent = noise_texture;
	noise_texture.image.onload = function(event) {
		handleLoadedTexture(event.currentTarget.parent);
	}
	noise_texture.image.src = "Images/noise.png";
	

}
			
function handleLoadedTexture(texture) {
		gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
		gl.bindTexture(gl.TEXTURE_2D, texture);
		gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);

		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR_MIPMAP_LINEAR);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
		gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
		gl.generateMipmap(gl.TEXTURE_2D);
		gl.bindTexture(gl.TEXTURE_2D, null);
}


